package io.renren.modules.generator.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import io.renren.modules.generator.entity.*;
import io.renren.modules.generator.service.TTrainingService;
import lombok.val;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.service.TTrainingCheckService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 考核信息表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-07 13:58:40
 */
@RestController
@RequestMapping("/ttrainingcheck")
public class TTrainingCheckController {
    @Autowired
    private TTrainingCheckService tTrainingCheckService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("generator:ttrainingcheck:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tTrainingCheckService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:ttrainingcheck:info")
    public R info(@PathVariable("id") Integer id){
		TTrainingCheckEntity tTrainingCheck = tTrainingCheckService.getById(id);

        return R.ok().put("tTrainingCheck", tTrainingCheck);
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:ttrainingcheck:update")
    public R update(@RequestBody TTrainingCheckEntity tTrainingCheck){
		tTrainingCheckService.updateById(tTrainingCheck);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:ttrainingcheck:delete")
    public R delete(@RequestBody Integer[] ids){
		tTrainingCheckService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 新增 考核信息
     */
    @RequestMapping("/save")
    //@RequiresPermissions("generator:ttrainingcheck:save")
    public R save(@RequestBody TTrainingCheckEntity tTrainingCheck){
        System.out.println("参数："+JSONObject.toJSONString(tTrainingCheck));
        tTrainingCheck.setId(null);
        Integer id=null;
        Integer trainingId=tTrainingCheck.getTrainingId();
        Integer chenkCount=tTrainingCheck.getChenkCount();
        if(trainingId==null || chenkCount==null){
            return R.error("训练ID和训练次数不能为空");
        }
        TTrainingCheckEntity tTrainingCheckEntity=tTrainingCheckService.selectByTrainingIdAndCount(trainingId,chenkCount);
        if(tTrainingCheckEntity==null){
            id=tTrainingCheckService.insertTTrainingCheck(tTrainingCheck);
        }else {
            id=tTrainingCheckEntity.getId();
            tTrainingCheck.setId(tTrainingCheckEntity.getId());
            tTrainingCheckService.updateTrainingCheck(tTrainingCheck);
        }
//        TTrainingScoreEntity trainingScoreEntity=new TTrainingScoreEntity();//导入传对象参数
//        trainingScoreEntity.setTrainingId(tTrainingCheck.getTrainingId());  //训练id
//        trainingScoreEntity.setFile(tTrainingCheck.getFile());    //路径
//        TTrainingScoreController s=new TTrainingScoreController();
//        s.imputTestdd(trainingScoreEntity);
        return R.ok().put("id",id);
    }

    /**
     * 新增 考核信息
     */
    @RequestMapping("/queryCheckId")
    //@RequiresPermissions("generator:ttrainingcheck:save")
    public R save(Integer trainingId,Integer checkCount){
        if(trainingId==null || checkCount==null){
            return R.error("训练ID和训练次数不能为空");
        }
        TTrainingCheckEntity tTrainingCheckEntity=tTrainingCheckService.selectByTrainingIdAndCount(trainingId,checkCount);
        if(tTrainingCheckEntity==null){
            return R.ok().put("id",null);
        }
        return R.ok().put("id",tTrainingCheckEntity.getId());
    }

    /**
     * 新增 考核信息
     */
    @RequestMapping("/queryCheckByTraining")
    //@RequiresPermissions("generator:ttrainingcheck:save")
    public R queryCheckByTraining(Integer trainingId){
        System.out.println("参数："+trainingId);
        if(trainingId==null ){
            return R.error("训练ID不能为空");
        }
        List<TTrainingCheckEntity> checkList=tTrainingCheckService.selectByTrainingId(trainingId);
        return R.ok().put("checkList",checkList);
    }






}
